//验证手机号
function isPoneAvailable($poneInput) {  
          var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;  
          if (!myreg.test($poneInput.val())) {  
              return false;  
          } else {  
              return true;  
          }  
      };
//验证邮编  
/*function is_postcode(postcode) {  
    	 if ( postcode == "") {  
    	        return false;  
    	    } else {  
    	        if (! /^[0-9][0-9]{5}$/.test(postcode)) {  
    	            return false;  
            }  
    	   }  
     return true;  
};*/
$('.phone').blur(function(){
	if(!isPoneAvailable($('.phone'))){
		this.value='';
		$('.message').html('请填写正确的手机号');
	}else{
		 $('.message').html('');
		return true;
	}
})   	  

//验证所有的信息填写完毕,不为空
function myCheck()
      {
        for(var i=0;i<$('input').length-1;i++)
        {
         if( $('input')[i].value=="")
         {
          $('.message').html("除邮编外,当前表单不能有空项");
          $('input')[i].focus();
           return false;
         }
        }
        $('.message').html('');
        return true;
        
};
//添加收货地址
var isSubmit = false;
$('.btn').click(function(){
	if(!myCheck()){
		return false;
	}
	if (isSubmit) {
		return;
	}
	
	var _receiverName = $("input[name=receiverName]").val();
	var _receiverPhone = $("input[name=receiverPhone]").val();
	var _receiverState = $("input[name=receiverState]").val();
	var _receiverCity = $("input[name=receiverCity]").val();
	var _receiverDistrict = $("input[name=receiverDistrict]").val();
	var _receiverAddress = $("input[name=receiverAddress]").val();
	var _receiverZip = $("input[name=receiverZip]").val();
	if(!(_receiverName&&_receiverPhone&&_receiverState&&_receiverCity&&_receiverDistrict&&_receiverAddress)){
		 $('.message').html("除邮编外,当前表单不能有空项哦");
		return;
	}
	$(".btn").attr({
		"disabled" : "disabled"
	});
	isSubmit = true;
	$.ajax({
		type : "POST",
		url : "/service/receiver/save",
		contentType : "application/x-www-form-urlencoded; charset=utf-8",
		data : {receiverName:_receiverName,receiverPhone:_receiverPhone,receiverState:_receiverState,receiverCity:_receiverCity,receiverDistrict:_receiverDistrict,receiverAddress:_receiverAddress,receiverZip:_receiverZip},
		dataType : 'json',
		success : function(result) {
			if(result.status == "200"){
				// 注册成功，去登录页
				$('.message').html('保存成功');
				$(".btn").removeAttr("disabled");
				isSubmit = false;
				return;
			}else{
				$('.message').html('保存失败');
			}
	    }
})

});
var isDel=false;
//删除收货地址
$('.del').click(function(){
	if (isDel) {
		return;
	}
	$(".del").attr({
		"disabled" : "disabled"
	});
	var _id = $(".del").attr("data-id");
	console.log(_id)
	isDel = true;
	$.ajax({
		type : "POST",
		url : "/service/receiver/delete?id="+_id,
		contentType : "application/x-www-form-urlencoded; charset=utf-8",
		data : {},
		dataType : 'json',
		success : function(result) {
			if(result.status == "200"){
				alert('删除成功');
				window.location.reload(); 
				isDel = false;
				return;
			}else{
				alert('删除失败');
			}
	    }
})

});
















